import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class ShowDAO {
    /**
     * 添加场次
     */
    public static void addShow(Show show) {
        String sql = "INSERT INTO shows (id, movie_id, hall_id, show_time, price) " +
                "VALUES (?, ?, ?, ?, ?)";

        try (Connection conn = DBUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {

            pstmt.setString(1, show.getId());
            pstmt.setString(2, show.getMovie().getId());
            pstmt.setString(3, show.getHall().getId());
            pstmt.setTimestamp(4, new java.sql.Timestamp(show.getShowTime().getTime()));
            pstmt.setDouble(5, show.getPrice());

            pstmt.executeUpdate();
        } catch (SQLException e) {
            System.err.println("添加场次失败: " + e.getMessage());
            throw new RuntimeException("添加场次失败", e);
        }
    }

    /**
     * 更新场次
     */
    public static void updateShow(Show show) {
        String sql = "UPDATE shows SET movie_id = ?, hall_id = ?, show_time = ?, price = ? WHERE id = ?";

        try (Connection conn = DBUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {

            pstmt.setString(1, show.getMovie().getId());
            pstmt.setString(2, show.getHall().getId());
            pstmt.setTimestamp(3, new java.sql.Timestamp(show.getShowTime().getTime()));
            pstmt.setDouble(4, show.getPrice());
            pstmt.setString(5, show.getId());

            pstmt.executeUpdate();
        } catch (SQLException e) {
            System.err.println("更新场次失败: " + e.getMessage());
            throw new RuntimeException("更新场次失败", e);
        }
    }

    /**
     * 获取所有场次
     */
    public static List<Show> getAllShows() {
        List<Show> shows = new ArrayList<>();
        String sql = "SELECT * FROM shows";

        try (Connection conn = DBUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql);
             ResultSet rs = pstmt.executeQuery()) {

            while (rs.next()) {
                String movieId = rs.getString("movie_id");
                String hallId = rs.getString("hall_id");

                Movie movie = MovieDAO.getMovieById(movieId);
                Hall hall = HallDAO.getHallById(hallId);

                if (movie != null && hall != null) {
                    Show show = new Show(
                            rs.getString("id"),
                            movie,
                            hall,
                            rs.getTimestamp("show_time"),
                            rs.getDouble("price")
                    );
                    shows.add(show);
                }
            }
        } catch (SQLException e) {
            System.err.println("查询所有场次失败: " + e.getMessage());
        }
        return shows;
    }

    /**
     * 根据ID查询场次
     */
    public static Show getShowById(String id) {
        String sql = "SELECT * FROM shows WHERE id = ?";

        try (Connection conn = DBUtil.getConnection();
             PreparedStatement pstmt = conn.prepareStatement(sql)) {

            pstmt.setString(1, id);
            ResultSet rs = pstmt.executeQuery();

            if (rs.next()) {
                String movieId = rs.getString("movie_id");
                String hallId = rs.getString("hall_id");

                Movie movie = MovieDAO.getMovieById(movieId);
                Hall hall = HallDAO.getHallById(hallId);

                if (movie != null && hall != null) {
                    return new Show(
                            rs.getString("id"),
                            movie,
                            hall,
                            rs.getTimestamp("show_time"),
                            rs.getDouble("price")
                    );
                }
            }
        } catch (SQLException e) {
            System.err.println("查询场次失败: " + e.getMessage());
        }
        return null;
    }
}
